package com.linlong.linlongchat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linlong.linlongchat.domain.po.Apply;
import com.linlong.linlongchat.domain.po.Message;
import com.linlong.linlongchat.mapper.MessageMapper;
import com.linlong.linlongchat.mapper.RecordMapper;
import com.linlong.linlongchat.service.IMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linlong.linlongchat.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 华少
 * @since 2024-09-20
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private RecordMapper recordMapper;
    @Override
    public List<Message> getMessageByRecordId(Integer recordId) {

        List<Message> messageList = lambdaQuery().eq(recordId != null, Message::getRecordId, recordId).orderByAsc(Message::getCreateTime).list();

        return messageList;

    }

    @Override
    public Long getCountByRecordId(Integer recordId) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String account = (String) claims.get("account");
        Long count = lambdaQuery().eq(recordId != null, Message::getRecordId, recordId)
                .eq(account != null, Message::getReceiveAccount, account).eq(Message::getStatus, 0).count();
        return count;
    }

    @Override
    public void updateStatusByRecordId(Integer recordId) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String receiveAccount = (String) claims.get("account");
        messageMapper.updateStatusByRecordId(recordId, receiveAccount);

    }

    @Override
    public boolean insertMessage(String targetId, String filePath) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userAccount = (String) claims.get("account");
        return false;
    }
}
